<?php

namespace app\models;

use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
use yii\data\Pagination;
use yii\data\ActiveDataProvider;
use yii\data\Sort;
use yii\helpers\Inflector;

class WxLogSearch extends WxLog
{

    public $region_name;
    public $user_name;


    public static function tableName()
    {
        return Order::tableName();
    }

    public function rules()
    {
        return [
//            [['region_id'], 'safe'],
//            [['region_name'], 'safe'],
            [['remark'], 'safe'],


        ];
    }
//    public function search($params, $prod_spec_id=null)
//    {
//
//    }

    public function search($params, $user_id=null, $state=null, $object_type_id=null,$cust_type_id=null,$reason=null, $create_user_id = null)
    {
        $where = [];
        $query = Order::find()->alias('a');
        $query->joinWith('users  AS b');
        $query->joinWith('custs  AS c');
        $query->joinWith('prodInsts  AS d');
        $query->joinWith('dealUsers  AS e');
        $query->joinWith('orderTypes  AS f');
        $query->joinWith('parentProdInsts  AS g');


        // 查询某人创建的所有订单
        if ($create_user_id)
        {
            $where['a.create_staff_id'] = $create_user_id;
        }

        if ($user_id)
        {

            //  如果是公盘 对应的员工标识填写的是0
//            if ($user_id==-1)
//            {
//                $where['a.staff_id'] = -1;
//            }
//            else{
                $where['a.staff_id'] = $user_id;
//            }

        }

        if ($reason)
        {

            $where['a.reason'] = $reason;

        }

        if ($state)
        {

            $where['a.state'] = $state;

        }

        if ( $object_type_id)
        {
            $where['a.object_type_id'] =  $object_type_id;
        }

        if ( $cust_type_id)
        {
            $where['c.cust_type_id'] =  $cust_type_id;
        }



        if ($where)
        {
            $query->where($where);
        }

        $sort = new Sort([
            'attributes' => [
                'modify_date',


            ],
        ]);




        $query->orderBy($sort->orders);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [
                'defaultOrder' => [
                    'modify_date' => SORT_DESC,
                ]
            ],
            'pagination' => [
                'pageSize' => 50,
            ],
        ]);



//        $sort = $dataProvider->getSort(); // 获取yii自动生成的排序规则
//        $sort->attributes['region_name'] = [ // 添加用户名的排序规则
//            'asc' => ['b.region_name' => SORT_ASC],
//            'desc' => ['b.region_name' => SORT_DESC],
//        ];
//
//
//        $dataProvider->setSort($sort);
//&& $this->validate()
        if (!($this->load($params) )) {
            return $dataProvider;
        }

        $query->andFilterWhere(['like', 'a.remark', $this->remark]);

//        $query->andFilterWhere(['like', 'c.cust_name', $this->cust_name]);
//        $query->andFilterWhere(['like', 'd.prod_name', $this->prod_name]);
//
//        $query->andFilterWhere(['like', 'd.username', $this->user_name]);
//        $query->andFilterWhere(['like', 'e.username', $this->staffName]);

        return $dataProvider;


    }


}

?>
